//
//  KSAgreementShowView.m
//  KunShanLife
//
//  Created by 蔺百好 on 2021/7/7.
//  Copyright © 2021 MrWang. All rights reserved.
//

#import "KSAgreementShowView.h"
@interface KSAgreementShowView () <UITextViewDelegate>
@property (weak, nonatomic) IBOutlet UIView *showContentView;
@property (weak, nonatomic) IBOutlet UIButton *disagreeBtn;

@property (weak, nonatomic) IBOutlet UIButton *agreeBtn;

@property (weak, nonatomic) IBOutlet UITextView *text_view;

@property (nonatomic, copy) NSString * private_xieyiUrl;
@property (nonatomic, copy) NSString * private_agreementUrl;

@end

@implementation KSAgreementShowView


+ (instancetype)loadFormXib {
    NSArray * array = [[NSBundle mainBundle]loadNibNamed:@"KSAgreementShowView" owner:nil options:nil];
    KSAgreementShowView * view = [array objectAtIndex:0];
    view.frame = UIScreen.mainScreen.bounds;
    return  view;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.private_xieyiUrl = @"http://share.yaodongqilai.com/#/termsService";
    self.private_agreementUrl = @"http://share.yaodongqilai.com/#/privacy";

    self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4];
    
    self.text_view.delegate = self;
   
  
    NSString * text = @"为了让您更好地使用“动起来Moving”, 请充分阅读并理解: 《用户协议》和《隐私政策》\n1、我们会遵循隐私政策收集、使用信息，但不会因同意了隐私政策而进行强制捆绑式的信息收集\n2、摄像头、相册、GPS等敏感权限均不会默认开启，只有经过明示授权才会在为实现功能或服务时使用，不会在功能或服务不需要时而通过您授权的权限收集信息。\n如果您同意，请点击下面的按钮已接受我们的服务。";

    NSMutableAttributedString * attribute = [[NSMutableAttributedString alloc]initWithString:text];
    NSMutableParagraphStyle * style = [[NSMutableParagraphStyle alloc] init];
    style.lineBreakMode = NSLineBreakByWordWrapping;
    style.lineSpacing = 8;
    [attribute addAttributes:@{NSParagraphStyleAttributeName:style} range:NSMakeRange(0, text.length)];
    [attribute addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]} range:NSMakeRange(0, text.length)];
    [attribute addAttributes:@{NSForegroundColorAttributeName:[UIColor lightGrayColor]} range:NSMakeRange(0, text.length)];
    
    
    [attribute addAttributes:@{NSLinkAttributeName:[NSURL URLWithString:self.private_xieyiUrl]} range:[text rangeOfString:@"《用户协议》"]];
    [attribute addAttributes:@{NSLinkAttributeName:[NSURL URLWithString:self.private_agreementUrl]} range:[text rangeOfString:@"《隐私政策》"]];
    
    self.text_view.attributedText = attribute;
    
}



- (IBAction)disagreeAct:(id)sender {
    exit(0);
}



- (IBAction)agreeAct:(UIButton *)sender {
    
    [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:@"bh_application_agreement_save"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    if (self.userAgreeActBlock) {
        self.userAgreeActBlock();
    }
    
    [self removeFromSuperview];
}


- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange interaction:(UITextItemInteraction)interaction {
    if ([URL.absoluteString isEqualToString:self.private_xieyiUrl] ||
        [URL.absoluteString isEqualToString:self.private_agreementUrl]) {
        
        if ([URL.absoluteString isEqualToString:self.private_xieyiUrl]) {
            if (self.agreementActBlock) {
                self.agreementActBlock(self.private_xieyiUrl, @"用户协议");
            }
        }else {
            if (self.agreementActBlock) {
                self.agreementActBlock(self.private_agreementUrl, @"隐私政策");
            }
        }
    }
    return NO;
}



@end
